import { NameSpace } from 'utils';
import exceed from 'utils/apimap';
import { Message } from '@teamix/ui';
import initCreateEntrance from 'utils/initNavigation/initCreateEntrance';
import intl from '../../../../../../../../../locale';

const { id, organization_id } = window.FORCE.organization;

const ns = NameSpace('organizationRepositoryPermissions');
export const RECEIVE_DATA = ns('RECEIVE_DATA');
export const SET_DATA = ns('SET_DATA');
export const GET_ORGALIASNAME = ns('GET_ORGALIASNAME');
export const UPDATE_DATA = ns('UPDATE_DATA');

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

// 查询企业设置
export function getRepoSettingData(cb) {
  return () => {
    exceed
      .forceFetch({
        api: 'organization.repo.setting.get',
        params: {
          org_id: id,
        },
      })
      .then((result) => {
        cb(result);
      });
  };
}

// 修改企业设置
export function formSubmit(formData, cb) {
  return () => {
    exceed
      .forceFetch({
        api: 'organization.repo.setting.post',
        params: {
          id,
        },

        data: formData,
      })
      .then(
        () => {
          Message.success({
            title: intl.get({
              id: 'code-assets.repositoryPermissions.actions.TheCodeLibrarySettingsHave',
              defaultMessage: '代码库设置更新成功',
            }),
          });

          cb && cb();
          // 更新导航配置
          initCreateEntrance();
        },
        () => {
          Message.error({
            title: intl.get({
              id: 'code-assets.repositoryPermissions.actions.UnableToUpdateCodeLibrary',
              defaultMessage: '代码库设置更新失败',
            }),
          });

          cb && cb();
        },
      );
  };
}

// 查询企业标识名设置
export function getOrgAliasName() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.alias.name.get',
        params: {
          orgId: organization_id,
        },
      })
      .then((result) => {
        dispatch({
          type: GET_ORGALIASNAME,
          data: result,
        });
      });
  };
}
